package csx.study.demo;

import csx.study.demo.dto.Result;
import csx.study.demo.dto.VoucherDTO;
import csx.study.demo.entity.Voucher;
import csx.study.demo.service.IVoucherService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

import static csx.study.demo.utils.RedisConstants.SECKILL_STOCK_KEY;

@SpringBootTest
public class RedisWarmUp {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IVoucherService voucherService;

    @Test
    public void warmUp() {
        Long shopId = 1L;

        // 直接调用接口里声明的方法
        Result result = voucherService.queryVoucherOfShop(shopId);
        // 如果需要拿到里面的 List<VoucherDTO>
        List<VoucherDTO> vouchers = (List<VoucherDTO>) result.getData();

        vouchers.forEach(v -> {
            String key = SECKILL_STOCK_KEY + v.getId();
            if(v.getStock()!=null&&v.getStock()>0){
                stringRedisTemplate.opsForValue().set(key, v.getStock().toString());
            }
        });
    }
}
